const TPL: string = `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据库字段字典</title>
    <style>
        .database {padding: 10px;border-bottom: 3px solid #e9e9e9;}
        .database .dbname {text-align: center;}
        .db-tables span {display: inline-block;margin-right: 40px;}
        .table-info{}
        table {border-collapse: collapse;outline: none;margin: 10px auto 40px;}
        th {background-color: #f1f1f1;word-wrap: break-word;word-break: break-all;}
        td, th {border: 1px solid #ccc;min-width: 50px;height: 20px;padding: 10px 20px;}
    </style>
</head>
<body>
<div>
   {{ dictionary }}
</div>
</body>
</html>`;

export function ExportDictionary(dictionary: any) {
    try {
        let tables = '';
        for (let dbname in dictionary) {
            tables += `<div class="database"><h2 class="dbname">${dbname} 数据字典</h2><div class="db-tables"><table contenteditable="true"><tbody>`;
            for (let tab in dictionary[dbname]) {
                let tabData = dictionary[dbname][tab];
                tables += `<tr><td style="background:#aeaeae;color: white;" colspan="6"><span>表名称:${tabData.table}</span><span>表引擎:${tabData.engine}</span><span>字符集:${tabData.collation}</span><span>表备注:${tabData.comment}</span></td></tr><tr><th>字段</th><th>类型</th><th>默认值</th><th>字符集</th><th>允许Null</th><th>备注</th></tr>`;
                for (let find of tabData.fields) {
                    tables += `<tr><td>${find.field}</td><td>${find.type}</td><td>${find.default}</td><td>${find.collation}</td><td>${find.is_null}</td><td>${find.comment}</td></tr>`;
                }
                tables += ''
            }
            tables += `</tbody></table></div></div>`;
        }
        const HTML = TPL.replace('{{ dictionary }}', tables)
        // 创建a标签
        let linkNode = document.createElement('a')
        linkNode.download = '数据字典.html';
        linkNode.style.display = 'none'
        // 利用Blob对象将字符内容转变成二进制数据
        let blob = new Blob([HTML])
        linkNode.href = URL.createObjectURL(blob)
        // 点击
        document.body.appendChild(linkNode)
        linkNode.click()
        // 移除
        document.body.removeChild(linkNode)
        return true
    } catch (e) {
        return false
    }
}